/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * MultiClassPanel.java
 *
 * Created on 2010-6-9, 23:37:24
 */
package GUI;

import Classifier.evaluation.ThresholdCurve;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.File;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JTable;
import javax.swing.UIManager;
import javax.swing.filechooser.FileFilter;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.JTableHeader;
import weka.classifiers.Classifier;
import weka.classifiers.Evaluation;
import weka.core.Attribute;
import weka.core.Instances;
import weka.core.converters.ConverterUtils.DataSource;

/**
 *
 * @author kevin
 */
public class MultiClassPanel extends javax.swing.JPanel {

    private Instances instances;
    private String[] classifiers;
    private String[] classifiersPath;
    private int[][] conMatrix;
    private double[][] output; //存储每个分类器的AUC、FMeasure、Precision、Recall及其平均值

    /** Creates new form MultiClassPanel */
    public MultiClassPanel() {
        initComponents();
        classifiersPath = new String[]{
                    "weka.classifiers.trees.J48",
                    "weka.classifiers.trees.NBTree",
                    "weka.classifiers.bayes.BayesNet",
                    "weka.classifiers.bayes.NaiveBayes"
                };
        classifiers = new String[]{"J48", "NBTree", "BayesNet", "NaiveBayes"};
        this.initialComboBox();

//        weka.classifiers.trees.J48 aa = new weka.classifiers.trees.J48();
//        weka.classifiers.trees.NBTree cc = new weka.classifiers.trees.NBTree();
//        weka.classifiers.bayes.BayesNet bb = new weka.classifiers.bayes.BayesNet();
//        weka.classifiers.bayes.NaiveBayes dd = new weka.classifiers.bayes.NaiveBayes();
    }

    private void test() {
        if (instances != null) {
            try {
                System.out.println(instances.numClasses());
                Attribute ca = instances.classAttribute();
                //test output the classes //
                for (int i = 0; i < ca.numValues(); i++) {
                    System.out.println(ca.value(i));
                }
                ////
                System.out.println(ca);
                //System.out.println(instances.attributeToDoubleArray(ca.index()).toString());
                int n = instances.numInstances();
                int k = instances.classIndex();
                for (int i = 0; i < instances.numInstances(); i++) {
                    for (int j = 0; j < instances.numAttributes(); j++) {
                        System.out.print(instances.instance(i).value(j) + "   ");
                    }
                    System.out.println();
                    //instances.instance(i).setValue(k, 1);
                }
                // matrixTable.setTableHeader(new JTableHeader());
            } catch (Exception ex) {
                ex.printStackTrace();
            }
        }
    }

    private void initialComboBox() {
        DefaultComboBoxModel a = new DefaultComboBoxModel(classifiers);
        this.classifierComboBox.setModel(a);

    }

    private void showConversionMatrix() {

        this.calculateConversionMatrix();
        //设置表头
        String[] columnNames = new String[conMatrix[0].length + 1];
        columnNames[0] = "Class";
        for (int i = 1; i < columnNames.length; i++) {
            columnNames[i] = "F" + i;
        }
        //设置表格信息
        String[][] data = new String[conMatrix.length][conMatrix[0].length + 1];
        //设置类别名
        Attribute ca = instances.classAttribute();
        int numValue = ca.numValues();
        for (int i = 0; i < data.length && i < numValue; i++) {
            //data[i][0]=instances.classAttribute()
            data[i][0] = ca.value(i);
        }

        for (int i = 0; i < data.length; i++) {
            for (int j = 1; j < data[1].length; j++) {
                data[i][j] = String.valueOf(conMatrix[i][j - 1]);
            }
        }
        ///
        DefaultTableModel matrixModel = new DefaultTableModel(data, columnNames);

        this.matrixTable.setModel(matrixModel);
        matrixTable.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
        matrixTable.setRowSelectionAllowed(true);
        matrixTable.setSelectionBackground(Color.lightGray);
        matrixTable.setSelectionForeground(Color.blue);
        matrixTable.doLayout();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        classifierPanel = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        classifierComboBox = new javax.swing.JComboBox();
        jLabel3 = new javax.swing.JLabel();
        cvfold = new javax.swing.JTextField();
        jLabel4 = new javax.swing.JLabel();
        startButton = new javax.swing.JButton();
        matrixPanel = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        matrixTable = new javax.swing.JTable();
        selectFilePanel = new javax.swing.JPanel();
        chooseFileButton = new javax.swing.JButton();
        filePathLabel = new javax.swing.JLabel();
        outputPanel = new javax.swing.JPanel();
        jScrollPane2 = new javax.swing.JScrollPane();
        outputTable = new javax.swing.JTable();

        classifierPanel.setBorder(javax.swing.BorderFactory.createTitledBorder(new javax.swing.border.LineBorder(new java.awt.Color(204, 204, 204), 1, true), "Classifiers"));
        classifierPanel.setName("classifierPanel"); // NOI18N

        jLabel1.setText("classifier:");

        classifierComboBox.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));

        jLabel3.setText("Cross-Validation:");

        cvfold.setText("10");

        jLabel4.setText("fold");

        startButton.setText("Start");
        startButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                startButtonActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout classifierPanelLayout = new javax.swing.GroupLayout(classifierPanel);
        classifierPanel.setLayout(classifierPanelLayout);
        classifierPanelLayout.setHorizontalGroup(
            classifierPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(classifierPanelLayout.createSequentialGroup()
                .addComponent(jLabel1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(classifierComboBox, javax.swing.GroupLayout.PREFERRED_SIZE, 108, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addComponent(jLabel3)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(cvfold, javax.swing.GroupLayout.PREFERRED_SIZE, 32, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jLabel4)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 14, Short.MAX_VALUE)
                .addComponent(startButton, javax.swing.GroupLayout.PREFERRED_SIZE, 73, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(33, 33, 33))
        );
        classifierPanelLayout.setVerticalGroup(
            classifierPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(classifierPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(classifierPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(classifierComboBox, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel3)
                    .addComponent(cvfold, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel4)
                    .addComponent(startButton))
                .addContainerGap(20, Short.MAX_VALUE))
        );

        matrixPanel.setBorder(javax.swing.BorderFactory.createTitledBorder(new javax.swing.border.LineBorder(new java.awt.Color(204, 204, 204), 1, true), "Conversion Matrix"));

        jScrollPane1.setAutoscrolls(true);

        matrixTable.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "CLASS", "CODON"
            }
        ));
        matrixTable.setColumnSelectionAllowed(true);
        jScrollPane1.setViewportView(matrixTable);
        matrixTable.getColumnModel().getSelectionModel().setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);

        javax.swing.GroupLayout matrixPanelLayout = new javax.swing.GroupLayout(matrixPanel);
        matrixPanel.setLayout(matrixPanelLayout);
        matrixPanelLayout.setHorizontalGroup(
            matrixPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(matrixPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 462, Short.MAX_VALUE)
                .addContainerGap())
        );
        matrixPanelLayout.setVerticalGroup(
            matrixPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(matrixPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 514, Short.MAX_VALUE)
                .addContainerGap())
        );

        selectFilePanel.setBorder(javax.swing.BorderFactory.createTitledBorder(new javax.swing.border.LineBorder(new java.awt.Color(204, 204, 204), 1, true), "Select File"));

        chooseFileButton.setText("choose file");
        chooseFileButton.setToolTipText("press to choose file");
        chooseFileButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                chooseFileButtonActionPerformed(evt);
            }
        });

        filePathLabel.setText("file path....");
        filePathLabel.setBorder(javax.swing.BorderFactory.createEtchedBorder(new java.awt.Color(204, 204, 204), new java.awt.Color(255, 255, 255)));

        javax.swing.GroupLayout selectFilePanelLayout = new javax.swing.GroupLayout(selectFilePanel);
        selectFilePanel.setLayout(selectFilePanelLayout);
        selectFilePanelLayout.setHorizontalGroup(
            selectFilePanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(selectFilePanelLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(chooseFileButton)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(filePathLabel, javax.swing.GroupLayout.DEFAULT_SIZE, 349, Short.MAX_VALUE)
                .addContainerGap())
        );
        selectFilePanelLayout.setVerticalGroup(
            selectFilePanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(selectFilePanelLayout.createSequentialGroup()
                .addGap(20, 20, 20)
                .addGroup(selectFilePanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(chooseFileButton)
                    .addComponent(filePathLabel))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        outputPanel.setBorder(javax.swing.BorderFactory.createTitledBorder(new javax.swing.border.LineBorder(new java.awt.Color(204, 204, 204), 1, true), "Classifier Output"));

        outputTable.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null, null}
            },
            new String [] {
                "null", "AUC", "Accuracy", "FMeasure", "Recall"
            }
        ) {
            boolean[] canEdit = new boolean [] {
                false, false, false, false, false
            };

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        jScrollPane2.setViewportView(outputTable);

        javax.swing.GroupLayout outputPanelLayout = new javax.swing.GroupLayout(outputPanel);
        outputPanel.setLayout(outputPanelLayout);
        outputPanelLayout.setHorizontalGroup(
            outputPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(outputPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        outputPanelLayout.setVerticalGroup(
            outputPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(outputPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 514, Short.MAX_VALUE)
                .addContainerGap())
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(matrixPanel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(classifierPanel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(outputPanel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(selectFilePanel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(selectFilePanel, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(classifierPanel, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(matrixPanel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(outputPanel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap())
        );
    }// </editor-fold>//GEN-END:initComponents

    private void chooseFileButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_chooseFileButtonActionPerformed
        try {
            JFileChooser fileChooser = new JFileChooser();
            fileChooser.addChoosableFileFilter(new FileFliter());
            fileChooser.showOpenDialog(this);
            File file = fileChooser.getSelectedFile();
            if (file != null) {
                String path = file.getPath();
                DataSource dataset = new DataSource(path);
                instances = dataset.getDataSet();
                if (instances.classIndex() == -1) {
                    instances.setClassIndex(instances.numAttributes() - 1);
                }
                this.filePathLabel.setText(path);
                this.showConversionMatrix();
            }
            //this.test();
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }//GEN-LAST:event_chooseFileButtonActionPerformed

    private void startButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_startButtonActionPerformed
        try {
            output = new double[conMatrix[0].length + 1][4];
            int index = this.classifierComboBox.getSelectedIndex();
            //load classifier//
            Class c = Class.forName(this.classifiersPath[index]);
            Classifier classifier = (Classifier) c.newInstance();
            if (classifier != null) {
                startEvaluating(classifier);
            }
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }//GEN-LAST:event_startButtonActionPerformed

    private void startEvaluating(Classifier classifier) {
        try {
            int fold = Integer.parseInt(this.cvfold.getText());
            int[] minIndex = this.getMinIndex();

            //根据转换矩阵构造分类器
            int num = instances.numInstances();//样例数
            Attribute classAtt = instances.classAttribute();//类别属性
            int numClasses = classAtt.numValues();//类别数
            int classIndex = instances.classIndex();
            double[] classes = new double[numClasses];//类别(用double表示)
            for (int i = 0; i < numClasses; i++) {
                classes[i] = (double) i;

            }
            for (int i = 0; i < conMatrix[0].length; i++) {//对转换矩阵的每一列进行分类器的构造
                Instances inst = new Instances(instances);
                List target = new ArrayList();
                int min = minIndex[i];
                for (int k = 0; k < conMatrix.length; k++) {//找到最少类的具体目标类
                    if (conMatrix[k][i] == min) {
                        target.add(classes[k]);
                    }
                }
                int size = target.size();
                for (int j = 0; j < num; j++) {//对数据集的每一行数据进行处理
                    int m = 0;
                    for (; m < size; m++) {//对所有的最少类
                        if (inst.instance(j).classValue() == (Double) target.get(m)) {
                            inst.instance(j).setValue(classIndex, 0.0);
                            break;//已经找到相等的了
                        }
                    }
                    if (m == size) {//循环结束了还没找到相等的类
                        inst.instance(j).setValue(classIndex, 1.0);
                        //inst.instance(j)
                    }
                    //该行数据修改操作结束，转入下一行数据
                }
                //所有数据都已进行修改，可以进行分类器的评估
                this.evaluateWithInstances(classifier, inst, fold, i);

                inst.delete();
            }
            //计算各个评估值的平均值//
            int n = conMatrix[0].length;
            if (n > 1) {
                for (int i = 0; i < output[0].length; i++) {
                    double sum = 0;
                    for (int j = 1; j < output.length; j++) {
                        sum += output[j][i];
                    }
                    output[0][i] = sum / n;
                }
            }
            this.showOutput();
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }

    private void showOutput() {
        //显示结果
        //设置表头
        String[] outputHeader = {"", "AUC", "Precision", "Recall", "FMeasure"};
        //设置分类器列
        String[][] data = new String[conMatrix[0].length + 1][5];
        data[0][0] = "Average";
        for (int i = 1; i < data.length; i++) {
            data[i][0] = "F" + i;
        }
        //设置数据
        for (int i = 0; i < data.length; i++) {
            for (int j = 0; j < output[0].length; j++) {
                data[i][j + 1] = String.valueOf(output[i][j]);
            }
        }

        DefaultTableModel model = new DefaultTableModel(data, outputHeader);

        outputTable.setModel(model);
        matrixTable.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);

        matrixTable.setRowSelectionAllowed(true);
        matrixTable.setSelectionBackground(Color.lightGray);
        matrixTable.setSelectionForeground(Color.blue);
        matrixTable.doLayout();
    }

    private int[] getMinIndex() {//找出转换矩阵中每一列里类别最少的标记
        int[] count0 = new int[conMatrix[0].length];
        int[] count1 = new int[conMatrix[0].length];
        int[] minIndex = new int[conMatrix[0].length];

        for (int i = 0; i < conMatrix.length; i++) {
            for (int j = 0; j < conMatrix[0].length; j++) {
                if (conMatrix[i][j] == 0) {
                    count0[j] += 1;
                } else {
                    count1[j] += 1;
                }
            }
        }
        for (int i = 0; i < conMatrix[0].length; i++) {
            if (count0[i] > count1[i]) {
                minIndex[i] = 1;
            } else {
                minIndex[i] = 0;
            }
        }
        return minIndex;
    }

    private void evaluateWithInstances(Classifier classifier, Instances inst, int fold) {
        this.evaluateWithInstances(classifier, inst, fold, 0);
    }

    private void evaluateWithInstances(Classifier classifier, Instances inst, int fold, int row) {
        try {
            Evaluation eval = new Evaluation(inst);
            eval.crossValidateModel(classifier, inst, fold, new Random(1));
            int classIndex = 0;

            double auc = eval.areaUnderROC(classIndex);
            output[row + 1][0] = auc;

            //recall/fmeasure and so on
            ThresholdCurve tc = new ThresholdCurve();
            Instances result = tc.getCurve(eval.predictions(), classIndex);

            //precision
            int index = result.attribute(ThresholdCurve.PRECISION_NAME).index();
            double[] precisions = result.attributeToDoubleArray(index);
            double sum = 0;
            for (int i = 0; i < precisions.length; i++) {
                sum += precisions[i];
            }
            output[row + 1][1] = sum / precisions.length;

            //recall
            index = result.attribute(ThresholdCurve.RECALL_NAME).index();
            double[] b = result.attributeToDoubleArray(index);
            sum = 0;
            for (int i = 0; i < b.length; i++) {
                sum += b[i];
            }
            output[row + 1][2] = sum / b.length;

            //FMeasure
            index = result.attribute(ThresholdCurve.FMEASURE_NAME).index();
            double[] a = result.attributeToDoubleArray(index);
            sum = 0;
            for (int i = 0; i < a.length; i++) {
                sum += a[i];
            }
            output[row + 1][3] = sum / a.length;


        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }

    String getTabTitleToolTip() {
        return "Process data with multiple classes";
    }

    String getTabTitle() {
        return "MultiClass process";
    }

    class FileFliter extends FileFilter {

        @Override
        public boolean accept(File f) {
            return f.getName().toLowerCase().endsWith(".arff") || f.isDirectory();
        }

        @Override
        public String getDescription() {
            return "arff file(.arff)";
        }
    }

    private void calculateConversionMatrix() {
        int n = instances.numClasses();//类别数，也是矩阵的行数
        //int n = 4;
        int c = (int) (Math.pow(2, n - 1) - 1);//矩阵的列数
        conMatrix = new int[n][c];
        //设置转换矩阵的值
        for (int i = 0; i < c; i++) {
            conMatrix[0][i] = 1;//矩阵的第一行全为1
        }
        for (int k = 1; k < n; k++) {//矩阵的第二行开始
            int num = (int) Math.pow(2, n - k - 1);
            //conMatrix[k][0]=0;
            for (int j = 0; j < c;) {//设置每一列的值
                for (int i = 0; i < num && j < c; i++) {
                    conMatrix[k][j] = 0;
                    j++;
                }
                for (int i = 0; i < num && j < c; i++) {
                    conMatrix[k][j] = 1;
                    j++;
                }
            }
        }
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton chooseFileButton;
    private javax.swing.JComboBox classifierComboBox;
    private javax.swing.JPanel classifierPanel;
    private javax.swing.JTextField cvfold;
    private javax.swing.JLabel filePathLabel;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JPanel matrixPanel;
    private javax.swing.JTable matrixTable;
    private javax.swing.JPanel outputPanel;
    private javax.swing.JTable outputTable;
    private javax.swing.JPanel selectFilePanel;
    private javax.swing.JButton startButton;
    // End of variables declaration//GEN-END:variables

    public static void main(String[] args) {
        try {
            UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
            final JFrame jf = new JFrame("MultiClass Process");
            MultiClassPanel panel = new MultiClassPanel();
            jf.getContentPane().setLayout(new BorderLayout());
            jf.getContentPane().add(panel, BorderLayout.CENTER);

            //jf.getContentPane()
            jf.addWindowListener(new WindowAdapter() {

                @Override
                public void windowClosing(WindowEvent e) {
                    jf.dispose();
                    System.exit(0);
                }
            });
            jf.pack();
            jf.setSize(1000, 700);
            jf.setVisible(true);
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }
}
